package com.ff.xyh.front.controller;

import com.ff.xyh.common.entity.userfront.FrontUser;
import com.ff.xyh.common.entity.userfront.UserRestricted;
import com.ff.xyh.common.entity.result.ResultObject;

import com.ff.xyh.front.service.UserFeignService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.websocket.server.PathParam;

/**
 * @Auther FF
 * @Date 2022年 11月 05日
 * @Time 00:51
 */
@Api(tags = "前端用户相关接口")
@Slf4j
@RestController
@RequestMapping("/user")
public class UserFrontFeignController {
    @Autowired
    private UserFeignService service;


    @ApiOperation(value = "登录")
    @PostMapping("login")
    ResultObject login(
            @ApiParam(value = "用户名/邮箱/手机号") @RequestParam(name = "userToken", required = false) String userToken,
            @ApiParam(value = "密码") @RequestParam(name = "password", required = false) String password) {
        System.out.println("收到的数据：" + userToken + password);
        return service.login(userToken, password);
    }
    @ApiOperation(value = "id获取基础信息")
    @PostMapping("getUserBaseById")
    ResultObject getUserBaseById(
            @ApiParam(value = "用户名/邮箱/手机号") @RequestParam(name = "id", required = false) String id) {
        return service.getUserBaseById(id);
    }
    @ApiOperation(value = "用token登录")
    @PostMapping("loginWithToken")
    ResultObject loginWithToken(
            @RequestParam(name = "token") String token) {
        return service.loginWithToken(token);
    }

    @ApiOperation(value = "注册")
    @PostMapping("register")
    ResultObject register(
            @RequestBody(required = false) FrontUser frontUser) {
        return service.register(frontUser);
    }

    @ApiOperation(value = "上传头像 如果是第二次上传 则需要在请求头avatar中加入上次上传返回的路径")
    @GetMapping("upload/avatar")
    ResultObject uploadAvatar(
            @RequestParam("avatar") MultipartFile multipartFile,
            HttpServletRequest request) {
        return service.uploadAvatar(multipartFile, request);
    }

    @ApiOperation(value = "获取当前登录的用户id, 昵称, 头像, 性别, token 需要在请求中携带token")
    @GetMapping("current")
    ResultObject getCurrentUser(HttpServletRequest request) {
            return service.getCurrentUser(request);
    }

    @ApiOperation(value = "通过id获取用户信息，仅测试用")
    @GetMapping("get/id/{id}")
    ResultObject getUserById(
            @PathVariable String id) {
        return service.getUserById(id);
    }

    @ApiOperation(value = "通过id获取用户信息，仅测试用")
    @GetMapping("get/openid")
    ResultObject getUserByOpenId(
            @RequestParam("openid") String openid) {
        return service.getUserByOpenid(openid);
    }

    @ApiOperation(value = "判断 用户名 是否已被注册")
    @GetMapping("hasUsername")
    ResultObject hasUserUsername(
            @RequestParam(name = "username") String username) {
        return service.hasUserUsername(username);
    }

    @ApiOperation(value = "判断 学号 是否已被注册")
    @GetMapping("hasSno")
    ResultObject hasUserSno(
            @RequestParam(name = "sno") String sno) {
        return service.hasUserSno(sno);
    }
    @ApiOperation(value = "判断 邮箱 是否已被注册")
    @GetMapping("hasEmail")
    ResultObject hasUserEmail(
            @RequestParam(name = "email") String email) {
        return service.hasUserEmail(email);
    }
    @ApiOperation(value = "判断 手机号 是否已被注册")
    @GetMapping("hasPhone")
    ResultObject hasUserPhone(
            @RequestParam(name = "phone") String phone) {
        return service.hasUserPhone(phone);
    }

    @ApiOperation(value = "更新用户信息，无法修改密码")
    @PostMapping("update")
    ResultObject updateUser (
            @RequestBody FrontUser frontUser) {
        return service.updateUser(frontUser);
    }

    @ApiOperation(value = "修改密码 需要在请求头中传入token")
    @GetMapping("update/{password}")
    ResultObject updateUserPasswordWithToken (
            @PathVariable String password,
            HttpServletRequest request) {
        return service.updateUserPasswordWithToken(password, request);
    }


    @ApiOperation(value = "根据id删除用户")
    @DeleteMapping("delete/{id}")
    ResultObject deleteUserById (
            @PathVariable String id) {
        return service.deleteUserById(id);
    }


    /**
     * 用户拉黑
     */
    @ApiOperation(value = "用户拉黑")
    @GetMapping("black/add")
    ResultObject insertUserBlack(
            @RequestParam(name = "id") String id,
            @RequestParam(name = "blackerId") String blackerId ) {
        return service.insertUserBlack(id, blackerId);
    }
    @ApiOperation(value = "取消拉黑")
    @GetMapping("black/delete")
    ResultObject deleteUserBlack(
            @RequestParam(name = "id") String id,
            @RequestParam(name = "blackerId") String blackerId) {
        return service.deleteUserBlack(id, blackerId);
    }

    /**
     * 用户收藏
     */
    // TODO 需要查询帖子，用户表获取其他信息
    @ApiOperation(value = "添加收藏")
    @PostMapping("favorite/add")
    ResultObject insertUserFavorite(
            @RequestParam(name = "id") String id,
            @RequestParam(name = "postId") String postId,
            @RequestParam(name = "postType") String postType ) {

        return service.insertUserFavorite(id, postId, postType);
    }

    @ApiOperation(value = "取消收藏")
    @PostMapping("favorite/delete")
    ResultObject deleteUserFavorite(
            @RequestParam(name = "id") String id,
            @RequestParam(name = "postId") String postId,
            @RequestParam(name = "postType") String postType ) {
        return service.deleteUserFavorite(id, postId, postType);
    }
    /**
     * 用户关注
     */
    @ApiOperation(value = "添加关注")
    @PostMapping("follow/add")
    ResultObject insertUserFollow(
            @RequestParam(name = "id") String id,
            @RequestParam(name = "followerId") String followerId ) {
        return service.insertUserFollow(id, followerId);
    }

    @ApiOperation(value = "取消关注")
    @PostMapping("follow/delete")
    ResultObject deleteUserFollow(
            @RequestParam(name = "id") String id,
            @RequestParam(name = "followerId") String followerId ) {
        return service.deleteUserFollow(id, followerId);
    }

    @ApiOperation(value = "根据id删除用户所有关注信息")
    @PostMapping("follow/delete/id")
    ResultObject deleteUserFollowById(
            @RequestParam(name = "id") String id ) {
        return service.deleteUserFollowById(id);
    }

    /**
     * 用户封号
     */
    @ApiOperation(value = "添加用户封号")
    @PostMapping("restrict/add")
    ResultObject userRestrict(UserRestricted userRestricted) {
        return service.userRestrict(userRestricted);
    }
    @ApiOperation(value = "删除用户封号")
    @PostMapping("restrict/delete")
    ResultObject userRestrictDelete(UserRestricted userRestricted) {
        return service.userRestrictDelete(userRestricted);
    }
}
